package adc.budget.impl.bs;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import adc.app.spec.IBo;
import adc.app.spec.IBoBs;
import adc.app.spec.IDao;

public class GenericBoBs<T extends IBo> implements IBoBs<T> {
    private IDao<T> dao;
    private final Class<T> boClass;

    public GenericBoBs(final Class<T> boClass, final IDao<T> dao) {
        this.boClass = boClass;
        this.dao = dao;
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public T create() {
        return dao.create(boClass);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void save(final T bo) {
        dao.save(bo);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void delete(final T bo) {
        dao.delete(bo);
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public T byId(final Long id) {
        return dao.byId(boClass, id);
    }

    @Override
    @Transactional(readOnly=true, propagation=Propagation.SUPPORTS)
    public List<T> list(Map<String, Object> filter, Integer firstIndex, Integer maxResultCount,
            Map<String, Boolean> order) {
        return dao.list(boClass, filter, firstIndex, maxResultCount, order);
    }

    @Override
    @Transactional(readOnly=true, propagation=Propagation.SUPPORTS)
    public Integer count(Map<String, Object> filter) {
        return dao.count(boClass, filter);
    }
}
